<script>
    function submitOnEnter(commandId, e)
    {
        var keycode;
        if (window.event) {
            keycode = window.event.keyCode;
        } else if (e) {
            keycode = e.which;
        } else  {
            return true;
        }
              
        if (keycode == 13) {
            document.getElementById(commandId).click();
            return false;
        } else {
            return true;
        }
    }
</script>

<%-- 
    Document   : nicks
    Created on : 19-oct-2008, 17:28:29
    Author     : org.@le

--%>

<%@taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
<%@taglib uri="http://richfaces.org/rich" prefix="rich"%>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
<c:if test="${sessionBean.ejbSession.owner==null}" >
            <jsp:forward page="../login.jsp" />
        </c:if>
        <c:if test="${!sessionBean.ejbSession.owner.adminUser}" >
            <jsp:forward page="../user/homeUser.jsp" />
        </c:if>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <f:view>
            <a4j:region>
            <h:form>
            <h:panelGrid width="100%">
                <rich:toolBar style="position:absolute; top: 10px; left: 10px" width="300px">
                    <a href="/site/faces/admin/admin.jsp" >Admin panel</a> > 
                    <a href="./countries.jsp" >Manage countries</a> > 
                    <a href="./sites.jsp" >Manage country's sites</a> > 
                    <a href="./areas.jsp" >Manage site's areas</a> > 
                    
                    <h:outputText value="Manage area's locations" />
                </rich:toolBar>
            </h:panelGrid>
                <br/>
                    <h1>Manage ${siteBean.area.area} locations</h1>
                <a4j:commandButton rendered="#{!siteBean.newLocation}" id="add" image="/images/add2.png" reRender="form1">
                    <f:setPropertyActionListener target="#{siteBean.newLocation}" value="#{true}" />
                    <f:setPropertyActionListener target="#{siteBean.currentLocation}" value="#{null}" />
                    <rich:toolTip direction="top-right" >
                        Add location
                    </rich:toolTip>
                </a4j:commandButton>
                <a4j:status>
                        <f:facet name="start">
                            <h:graphicImage  value="/images/ajax_process.gif"/>
                        </f:facet>
                    </a4j:status>
            </h:form>
                <br/>
            <h:form id="form1">
                <h:messages id="mal1" globalOnly="true"/>
                <rich:panel rendered="#{siteBean.newLocation}">
                    <div align="right" >
                        <a4j:commandButton id="save" value="Save" action="#{siteBean.saveLocation}" type="submit" reRender="form1, formLocations"/>
                        <a4j:commandButton id="cancel" value="Cancel" immediate="true" reRender="form1">
                            <f:setPropertyActionListener target="#{siteBean.newLocation}" value="#{false}" />
                            <f:setPropertyActionListener target="#{siteBean.currentLocation}" value="#{null}" />
                        </a4j:commandButton>
                        
                    </div>
                    <h:panelGrid columns="3" >
                        <h:outputText value="(*) Location: " />
                        <h:inputText maxlength="40" id="location" requiredMessage="This field is required." required="true" value="#{siteBean.currentLocation.location}" onkeypress="return submitOnEnter('form1:save',event);"/>
                        <h:message for="location" style="color: red" styleClass="errorMessage" />
                        <h:outputText value="Description: " />
                        <h:inputText maxlength="100" value="#{siteBean.currentLocation.description}" onkeypress="return submitOnEnter('form1:save',event);"/>
                        <h:outputText value="" />
                    </h:panelGrid>
                </rich:panel>
            </h:form>
            <h:form id="formLocations">
                <h:outputText value="No locations were created." rendered="#{empty siteBean.currentAreaLocations}"/>
                <c:if test="${!empty siteBean.currentAreaLocations}">
                <rich:dataTable id="locationsTable" columns="6" rows="30" value="#{siteBean.currentAreaLocations}" var="location" border="1" >
                    <rich:column>
                        <f:facet name="header">
                            <h:outputText value="Locations" />
                        </f:facet>
                        <h:panelGrid id="column1" >
                            <h:outputText value="#{location.location}" />
                        </h:panelGrid>
                    </rich:column>
                    <rich:column>
                        <f:facet name="header">
                            <h:outputText value="Description" />
                        </f:facet>
                        <h:panelGrid >
                            <h:outputText value="#{location.description}" />
                        </h:panelGrid>
                    </rich:column>
                    
                    <rich:column>
                        <f:facet name="header">
                        </f:facet>
                        <h:panelGrid >
                            <a4j:commandButton id="add1" image="/images/edit.png" reRender="form1">
                                <f:setPropertyActionListener target="#{siteBean.newLocation}" value="#{true}" />
                                <f:setPropertyActionListener target="#{siteBean.currentLocation}" value="#{location}" />
                                <rich:toolTip direction="bottom-right" >
                                    Edit
                                </rich:toolTip>
                            </a4j:commandButton>
                            
                        </h:panelGrid>
                    </rich:column>
                    <rich:column>
                        <f:facet name="header">
                        </f:facet>
                        <h:panelGrid  >
                            <a4j:commandButton id="add2" image="/images/trash.png" action="#{siteBean.removeLocation}" reRender="form1, formLocations">
                                <f:setPropertyActionListener target="#{siteBean.currentLocation}" value="#{location}" />
                                <rich:toolTip direction="bottom-right" >
                                    Delete
                                </rich:toolTip>
                            </a4j:commandButton>
                            
                        </h:panelGrid>
                    </rich:column>
                    
                    
                </rich:dataTable>
                       <rich:datascroller for="locationsTable" maxPages="5" page="#{sessionBean.pagePostsUser}" />
                       </c:if>
            </h:form>
            </a4j:region>
        </f:view>
    </body>
</html>
